home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 April / april_2001.iso / intercd / root / ^Palm / Games / eCross / src / MiniString.java < prev    next >
Encoding:
Java Source  |  2000-07-25  |  3.6 KB  |  157 lines

  1. /*
  2.  * MiniString.java - A string displayed using a mini font. Numbers only
  3.  * Copyright (C) 2000 Romain Guy
  4.  * guy.romain@bigfoot.com
  5.  * www.jext.org
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License
  9.  * as published by the Free Software Foundation; either version 2
  10.  * of the License, or any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  */
  21.  
  22. import waba.fx.*;
  23. import waba.sys.*;
  24.  
  25. /**
  26.  * This class is used to display a string made of numbers and
  27.  * whitespaces using a tiny font.
  28.  * @author Romain Guy <guy.romain@bigfoot.com>
  29.  * @version 1.0
  30.  */
  31.  
  32. public class MiniString
  33. {
  34.   // contains the images of the chars
  35.   public static Image[] FONT_CHARS = new Image[16];
  36.   // font constants
  37.   /** Font width in pixels. **/
  38.   public static final int FONT_WIDTH = 5;
  39.   /** Font height in pixels. **/
  40.   public static final int FONT_HEIGHT = 5;
  41.  
  42.   // mini string modes
  43.   /** Horizontal display. **/
  44.   public static final byte HORIZONTAL = 0;  
  45.   /** Vertical display. **/
  46.   public static final byte VERTICAL = 1;
  47.  
  48.   // the text to be displayed
  49.   private byte[] datas;
  50.   private int length;
  51.   // mode
  52.   private byte mode;
  53.  
  54.   /**
  55.    * Creates a new mini string.
  56.    * @param text The text to be displayed
  57.    */
  58.  
  59.   public MiniString(byte[] datas, byte mode)
  60.   {
  61.     this.datas = datas;
  62.     this.mode = mode;
  63.     this.length = this.datas.length;
  64.   }
  65.  
  66.   /**
  67.    * Free the resources occupied by the font.
  68.    */
  69.  
  70.   public static void free()
  71.   {
  72.     for (int i = 0; i < FONT_CHARS.length; i++)
  73.       FONT_CHARS[i].free();
  74.   }
  75.  
  76.   // returns the requested image. if it does not exist,
  77.   // the image is created and then stored for a future
  78.   // use.
  79.  
  80.   private static Image getImage(int _image)
  81.   {
  82.     Image image = FONT_CHARS[_image];
  83.  
  84.     if (image == null)
  85.     {
  86.       image = new Image("datas/font" + _image + ".bmp");
  87.       FONT_CHARS[_image] = image;
  88.     }
  89.  
  90.     return image;
  91.   }
  92.  
  93.   /**
  94.    * Draws the text.
  95.    * @param x The x coordinate
  96.    * @param y The y coordinate
  97.    * @param g The drawing area
  98.    */
  99.  
  100.   public void drawText(int x, int y, Graphics g)
  101.   {
  102.     int c;
  103.     int spaces = 0, lines = 0, width = 0;
  104.  
  105.     for (int i = 0; i < length; i++)
  106.     {
  107.       c = datas[i];
  108.       g.drawImage(getImage(c), x + (width * FONT_WIDTH) + spaces, y + lines * (FONT_HEIGHT + 2));
  109.  
  110.       switch (mode)
  111.       {
  112.         case HORIZONTAL:
  113.           spaces += 2;
  114.           width++;
  115.           break;
  116.         case VERTICAL:
  117.           lines++;
  118.           break;
  119.       }
  120.     }
  121.   }
  122.  
  123.   /**
  124.    * Returns the width of the text.
  125.    */
  126.  
  127.   public int getTextWidth()
  128.   {
  129.     switch (mode)
  130.     {
  131.       case HORIZONTAL:
  132.         return length * (FONT_WIDTH + 2);
  133.       case VERTICAL:
  134.         return FONT_WIDTH;
  135.     }
  136.     return -1;
  137.   }
  138.  
  139.   /**
  140.    * Returns the height of the text.
  141.    */
  142.  
  143.   public int getTextHeight()
  144.   {
  145.     switch (mode)
  146.     {
  147.       case VERTICAL:
  148.         return length * (FONT_HEIGHT + 2);
  149.       case HORIZONTAL:
  150.         return FONT_HEIGHT;
  151.     }
  152.     return -1;
  153.   }
  154. }
  155.  
  156. // End of MiniString.java
  157.